package itsm.isperp.module.service.problem;

import itsm.isperp.framework.data.domain.DataResponse;
import itsm.isperp.framework.service.BaseService;
import itsm.isperp.framework.web.request.JqGridRequest;
import itsm.isperp.module.dto.project.ProjectDomain;
import itsm.isperp.module.dto.project.ProjectDomain.ProjectDomainType;
import itsm.isperp.module.entity.problem.ProbProblem;
import itsm.isperp.module.repository.problem.ProbProblemMapper;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Random;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

/**
 * 
 * 
 * 
 * @author lizx
 * @date 2014-7-18
 * @since 1.0
 * 
 */
@Service
public class ProbProblemService extends BaseService<ProbProblem> {

  @Autowired
  protected ProbProblemMapper probProblemMapper;

  @Autowired
  protected JdbcTemplate jdbcTemplate;

  @Override
  public ProbProblemMapper getRepository() {
    return probProblemMapper;
  }

  public DataResponse<Map<String, Object>> searchAllByFilter(
      JqGridRequest request) {
    request.setProjectDomain(ProjectDomain.newInstance("project_id",
        ProjectDomainType.Technology));
    return probProblemMapper.findAllDetailByRequest(request);
  }
  
  public Date dealDate(Date sourceDate)
  {
    Date result=sourceDate;
    int hour=result.getHours();
    if(hour>18 || hour<8)
    {
      Calendar cal = Calendar.getInstance();   
      cal.setTime(sourceDate);   
      cal.add(Calendar.HOUR, 6);// 24小时制   
      sourceDate = cal.getTime();
      result=dealDate(sourceDate);
    }
    return result;
  }

  public String doUpdateTime(String code, Date createTime) {
    String sql11 = "select * from prob_problem where code='" + code
        + "' limit 1";
    String process_id = "";
    String id = "";
    List<Map<String, Object>> list11 = jdbcTemplate.queryForList(sql11);
    id = list11.get(0).get("id").toString();
    process_id = list11.get(0).get("process_id").toString();

    String sql0 = "SELECT * FROM bpm_action_history WHERE process_id='"
        + process_id + "' ORDER BY task_id";
    List<Map<String, Object>> list = jdbcTemplate.queryForList(sql0);
    Date lastUpdate = createTime;
    long beginTime = createTime.getTime();
    for (int i = 0; i < list.size(); i++) {
      String actionId = list.get(i).get("id").toString();
      if (i > 0) {
        long distributeTime = lastUpdate.getTime() + getRandomNum(1, 300)
            * 1000 * 60;
        lastUpdate = new Date(distributeTime);
        lastUpdate = dealDate(lastUpdate);

      }
      String sql1 = "update bpm_action_history set action_time=? where id=?";
      jdbcTemplate.update(sql1, new Object[] { lastUpdate, actionId });
    }
    Date solvedDate = createTime;

    String sql3 = "SELECT action_time FROM bpm_action_history where action_name='问题验证' and process_id='"
        + process_id + "'";
    List<Map<String, Object>> list3 = jdbcTemplate.queryForList(sql3);
    if (list3.size() > 0) {
      Date d3 = (Date) list3.get(0).get("action_time");
      solvedDate = d3;
    }

    // 更新事件编号
    String tnewCode = code.substring(7, 11);
    SimpleDateFormat sFormat = new SimpleDateFormat("yyMM");
    String codeT = sFormat.format(createTime);
    String newCode = "PROB" + codeT + tnewCode;

    String sql6 = "UPDATE prob_problem SET request_date='"
        + createTime.toLocaleString() + "',execute_time='"+solvedDate+"',create_date='"
        + createTime.toLocaleString() + "',code='" + newCode + "' where id='"
        + id + "'";
    jdbcTemplate.update(sql6);
    return "ok";
  }
  
  // 产生随机数
  public int getRandomNum(int beg, int end) {
    Random random = new Random(System.currentTimeMillis());
    int ret = random.nextInt(end - beg) + beg;
    return ret;
  }

}
